<template>
  <div class="video-control">
    <h2>视频流控制</h2>
    <input v-model="videoUrl" placeholder="输入视频流地址" />
    <button @click="startVideoStream">开始视频拉流</button>
    <button @click="stopVideoStream">停止视频拉流</button>
    <video ref="videoPlayer" autoplay controls></video>
  </div>
</template>

<script setup>
import {ref} from 'vue';
import axios from 'axios';

const videoUrl = ref('');
const videoConnection = ref(null);
const videoPlayer = ref(null);

async function startVideoStream() {
  const whepUrl = 'http://106.54.211.74:1985/rtc/v1/whep/?app=live&stream=livestreamf3';

  videoConnection.value = new RTCPeerConnection();
  videoConnection.value.addTransceiver('video', {direction: 'recvonly'});
  videoConnection.value.addTransceiver('audio', {direction: 'recvonly'});

  videoConnection.value.onicecandidate = (event) => {
    if (event.candidate) {
      console.log('Video ICE Candidate:', event.candidate);
    }
  };

  videoConnection.value.ontrack = (event) => {
    if (event.streams && event.streams[0]) {
      videoPlayer.value.srcObject = event.streams[0];
    }
  };

  try {
    const offer = await videoConnection.value.createOffer();
    await videoConnection.value.setLocalDescription(offer);

    const response = await axios.post(whepUrl, offer.sdp, {
      headers: {'Content-Type': 'application/sdp'}
    });

    const answer = new RTCSessionDescription({
      type: 'answer',
      sdp: response.data
    });
    await videoConnection.value.setRemoteDescription(answer);
  } catch (error) {
    console.error('视频流拉取失败:', error);
    stopVideoStream();
  }
}

function stopVideoStream() {
  if (videoConnection.value) {
    videoConnection.value.close();
    videoConnection.value = null;
  }
  if (videoPlayer.value) {
    videoPlayer.value.srcObject = null;
    videoPlayer.value.pause();
  }
}
</script>

<style scoped>
.video-control {
  border: 1px solid #e2e8f0;
  padding: 2rem;
  border-radius: 12px;
  background: #ffffff;
  flex: 1;
  min-width: 280px;
  max-width: 1200px;
  margin: 2rem auto;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

h2 {
  color: #1a365d;
  margin-bottom: 1.5rem;
  font-size: 1.5rem;
  font-weight: 600;
}

input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  margin-bottom: 1rem;
  transition: border-color 0.2s;
}

input:focus {
  outline: none;
  border-color: #4299e1;
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.2);
}

button {
  background-color: #4299e1;
  color: white;
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 6px;
  margin-right: 1rem;
  cursor: pointer;
  transition: all 0.2s;
}

button:hover {
  background-color: #3182ce;
}

button:active {
  transform: scale(0.98);
}

button:disabled {
  background-color: #cbd5e0;
  cursor: not-allowed;
}

video {
  width: 100%;
  max-width: 800px;
  margin: 2rem auto;
  display: block;
  border-radius: 12px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

@media (max-width: 768px) {
  .video-control {
    padding: 1rem;
    margin: 1rem;
  }

  h2 {
    font-size: 1.25rem;
  }

  button {
    width: 100%;
    margin-bottom: 0.5rem;
  }
}
</style>
